using System;
using System.Data;
using Dimok.Data.Database;
using Dimok.Data;
using ConfigDB = Dimok.Config.db;
using Dimok.Exceptions;

namespace Dimok.Verification {

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Ws. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class WS{

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Adds an item information. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="xmlRS">	Element describing the xml rs. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void AddItemInfo(System.Xml.XmlElement xmlRS) {
			AddItemInfo(xmlRS, new UserObjectEx().menuID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Adds an item information. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="xmlRS">	Element describing the xml rs. </param>
		/// <param name="menuID">	Identifier for the menu. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void AddItemInfo(System.Xml.XmlElement xmlRS,int menuID) {
			AddItemInfo(SQLHelperEx.LoadDataSet(xmlRS),menuID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Adds an item information. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="lable">	The lable. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void AddItemInfo(string lable) {
			DataTable dt = new DataSet().Tables.Add();
			dt.Columns.Add("Lable");
			dt.Rows.Add(new object[]{lable});
			AddItemInfo(dt.DataSet);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Adds an item information. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="dsItem">	The ds item. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void AddItemInfo(DataSet dsItem) {
			AddItemInfo(dsItem, new UserObjectEx().menuID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Adds an item information. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="dsItem">	The ds item. </param>
		/// <param name="menuID">	Identifier for the menu. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void AddItemInfo(DataSet dsItem,int menuID) {
			UserObjectEx uox = new UserObjectEx();
			DataTable dtItem = dsItem.Tables[0];
			DataRow rowItem = dtItem.Rows[0];
			if( rowItem.Table.Columns.Contains("ParentID") && (rowItem["ParentID"]+"") == "0" )
				SQLHelperEx.EnsureColumn(rowItem,"MenuID",menuID);
			int id = dtItem.Columns.Contains("ID") ? int.Parse(rowItem["ID"].ToString()) : 0;
			if( dtItem.Columns.Contains("IsCustom") && int.Parse(rowItem["IsCustom"]+"") != 0 ){
				if( !dtItem.Columns.Contains(ConfigDB.ApplID) )
					rowItem[dtItem.Columns.Add(ConfigDB.ApplID,typeof(int))] = uox.applID;
				SQLHelperEx.UpdateDataSet(dsItem,uox.db,
					@"SELECT "+ConfigDB.ApplID+@", VerificationID, ParentID, Lable, Checked, Pos, MenuID,Description,
						CommentToManager, CommentFromBroker, CommentToVerifier, CommentToVerifierHist,
						CommentToSelf, CommentToManager_Date, ManagerTo, IsParent, IsCustom,
						Owner, CommentToBroker AS descToBroker FROM t_"+ConfigDB.ApplName+@"_Verification
						WHERE "+ConfigDB.ApplID+"=@AID AND VerificationID=@VID",uox.applID,id);
			}
			else{
				SQLHelperEx.UpdateDataSet(dsItem,uox.db,"SELECT * FROM t_Verification WHERE ID=@ID",id);
				//SQLHelperEx.ExecuteNonQueryEx(uox.db,"s_Applicant_UpdateVerTree",uox.applID);
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the ver information described by xmlRS. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="xmlRS">	Element describing the xml rs. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string UpdateVerInfo(System.Xml.XmlElement xmlRS) {
			UserObjectEx uox = new UserObjectEx();
			DataSet dsMsg = SQLHelperEx.LoadDataSet(xmlRS);
			int verID = int.Parse(dsMsg.Tables[0].Rows[0]["ID"].ToString());
			SQLHelperEx.UpdateDataSet(dsMsg,uox.db,"SELECT * FROM t_"+ConfigDB.ApplName+"_Verification WHERE "+ConfigDB.ApplID+"=@ApplID AND VerificationID=@VerID",uox.applID,verID);
			return uox.VerificationItemsDS(verID).GetXml();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Level item up. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="ID">	The identifier. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void LevelItemUp(int ID){
			UserObjectEx uox = new UserObjectEx();
			SQLHelperEx.ExecuteNonQuery(uox.db,"s_VerItem_LevelUp",ID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Copies the item. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="ID">				The identifier. </param>
		/// <param name="parentID">	Identifier for the parent. </param>
		/// <param name="afterID">	Identifier for the after. </param>
		/// <param name="menuID">		Identifier for the menu. </param>
		/// <param name="move">			true to move. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void CopyItem(int ID,int parentID,int afterID,int menuID,bool move){
			UserObjectEx uox = new UserObjectEx();
			SQLHelperEx.ExecSP(uox.db,180,"s_VerItem_Copy",ID,parentID,afterID,parentID == 0 && afterID == 0?uox.menuID:menuID,move);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Deletes the ver item described by ID. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="ID">	The identifier. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void DeleteVerItem(int ID) {
			UserObjectEx uox = new UserObjectEx();
			SQLHelperEx.ExecuteNonQueryEx(uox.db,@"DELETE t_Verification WHERE ID=@ID",ID);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the checked. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ASPException">	Thrown when asp. </exception>
		///
		/// <param name="ID">					The identifier. </param>
		/// <param name="isChecked">	true if is checked. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string UpdateChecked(int ID,bool isChecked){
			try {
				UserObjectEx uox = new UserObjectEx();
				return SQLHelperEx.ExecuteDatasetEx(uox.db, "EXEC sp__sec_Verification_Update @ApplID,@ID,@Chk", uox.applID, ID, isChecked).GetXml();
			} catch (Exception exc) { throw new ASPException(exc); }
		}

		// Info

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Verification item. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="verID">	Identifier for the ver. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string VerificationItem(int verID) {
			UserObjectEx uox = new UserObjectEx();
			return uox.VerificationItemsDS(verID).GetXml();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the verification items. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string VerificationItems() {
			UserObjectEx uox = new UserObjectEx();
			return uox.VerificationItemsDS().GetXml();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the verification tree xml. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string VerificationTreeXML() {
			return new UserObjectEx().VerificationTreeXML();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the verification tree xml. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="msg">	The message. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string VerificationTreeXML(int msg) {
			return new UserObjectEx().VerificationTreeXML(msg);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Ver item hist. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="verID">	Identifier for the ver. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string VerItemHist(int verID){
				UserObjectEx uox = new UserObjectEx();
				return SQLHelperEx.XmlInnerText(uox.db,"SELECT * FROM dbo.fverTree_Hist("+uox.applID+","+verID+")");
		}

	}
}
